package com.wangshidai.t2;

import java.io.*;
import java.net.Socket;

public class HttpMessageUtil {

    //读取消息
    public static String readMessage(Socket socket){
        String message ="";
        try {
            //接收客户端的消息
            InputStream inputStream = null;
            inputStream = socket.getInputStream();
            InputStreamReader isr=new InputStreamReader(inputStream);
            BufferedReader br=new BufferedReader(isr);

            if (inputStream.available()>0){//有消息就处理

                message = br.readLine();//读取消息
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;

    }
    //发送消息
    public static void sendMessage(Socket socket,String message){
        try {
            //发送消息给客户端
            //发送消息就是流的操作
            OutputStream os = null;
            os = socket.getOutputStream();
            PrintStream ps=new PrintStream(os,true);
            ps.println(message);  //发送消息



        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
